//
//  BGPublicAnnoucementViewController.m
//  bigMall
//
//  Created by 大魔王 on 2017/1/11.
//  Copyright © 2017年 thinklev. All rights reserved.
//

#import "BGPublicAnnoucementViewController.h"
#import "BGDateResult.h"
#import "BGNoticeListModel.h"
#import "BGAdversitingCell.h"
#import "BGWebController.h"

@interface BGPublicAnnoucementViewController () <UITableViewDelegate,UITableViewDataSource> {
    
    UIImageView *imageView;
    UILabel *lable;
    int index;
    NSMutableArray *_dataArr;
    
}
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation BGPublicAnnoucementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //     self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = self.titleStr;
    self.view.backgroundColor = [UIColor whiteColor];
    [self initDat];
    [self loadInter];
    [self createUi];
    
}
- (void)initDat {
    
    index = 1;
    
}
- (void)createUi {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    //刷新的加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [self.tableView.header beginRefreshing];
    //判断数据源有无
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (self.tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [self.tableView addSubview:lable];
    
    [self.tableView addSubview:imageView];
    lable.hidden = YES;
    imageView.hidden = YES;
    
}
- (void)addIndex {
    
    index++;
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}
- (void)loadInter {
    
    NSLog(@"type:%@",self.cateStr);
    
    [BGDateResult brandListWithType:self.cateStr thisPage:[NSString stringWithFormat:@"%ld",(long)index] pageNum:@"10" success:^(NSObject *result) {
        
        BGNoticeListModel *model = (BGNoticeListModel *)result;
        _dataArr = [NSMutableArray arrayWithArray:model.list];
        if (_dataArr.count == 0 && index == 1) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshingWithNoMoreData];
            
            //判断数据源有无
            lable.hidden = NO;
            imageView.hidden = NO;
            
        }
        NSLog(@"%@",model.allPage);
        if ([model.allPage intValue] > index) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
        if (model.list.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
            return;
        }
        
        lable.hidden = YES;
        imageView.hidden = YES;
        [self.tableView.footer endRefreshing];
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
        

    } failure:^(NSError *error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 77;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView registerClass:[BGAdversitingCell class] forCellReuseIdentifier:@"celId" ];
    BGAdversitingCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    BGNoticeListModelList *model = [[BGNoticeListModelList alloc] init];
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        cel.nameStr = model.title;
        cel.contStr = model.description1;
        cel.timeStr = model.create_time;
        cel.countStr = model.view;
        cel.authorStr = model.auther;
        
    }
    
    return cel;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BGNoticeListModelList *model = [[BGNoticeListModelList alloc] init];
    if (_dataArr.count != 0) {
        model = _dataArr[indexPath.row];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BGWebController *web = [[BGWebController alloc] init];
    web.url = [NSString stringWithFormat:@"http://thinkpro.thinklev.com/api_1/article/articleDetail/id/%@",model.idStr];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}

- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    //    [UIApplication sharedApplication].statusBarHidden = YES;
}
@end
